package be.kuleuven.peno3.mobiletoledo.view.multiplechoice;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.LayoutParams;
import android.widget.TextView;
import be.kuleuven.peno3.mobiletoledo.R;
import be.kuleuven.peno3.mobiletoledo.Data.Client.MultipleChoiceClient;
import be.kuleuven.peno3.mobiletoledo.model.Question;

public class AnswerQuestion extends Activity {
	private RadioGroup choices;
	private TextView questiontxt;
	private Button answer;
	private Question question;
	private boolean canBeAnswered = true;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.multiplechoiceanswerquestion);
		
		Bundle b = getIntent().getExtras();
		int questionId = (Integer) b.get("questionId");
		
		question = MultipleChoiceClient.getQuestion(questionId);
		try {
			question.equals(null);
		}
		catch (NullPointerException ex) {
			ex.printStackTrace();
			Toast.makeText(AnswerQuestion.this.getApplicationContext(), "Er is geen internetverbinding", Toast.LENGTH_SHORT).show();
			finish();
			return;
		}
		
		choices=(RadioGroup) findViewById(R.string.choices);
		questiontxt=(TextView) findViewById(R.string.question);
		answer= (Button) findViewById(R.string.answerquestion);
		
		questiontxt.setText(question.getContent());

		String[] choicestxt = question.getPossibleAnswers();
		
		for (int i = 0; i<choicestxt.length;i++) {
			RadioButton r = new RadioButton(this);
			r.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
			r.setText(choicestxt[i]);
			r.setTextSize(TypedValue.COMPLEX_UNIT_PX, 18);
			r.setId(i);
			choices.addView(r, i);
		}
		
		answer.setOnClickListener(new View.OnClickListener() {
	        public void onClick(View v) {
	        	int answerid = choices.getCheckedRadioButtonId();
	        	if(answerid<0) {
	        		Toast.makeText(AnswerQuestion.this.getApplicationContext(), "Gelieve de vraag eerst te beantwoorden", Toast.LENGTH_SHORT).show();
	        		return;
	        	}
	        	if(!MultipleChoiceClient.answerQuestion(question.getId(), answerid)) {
	        		Toast.makeText(AnswerQuestion.this.getApplicationContext(), "Er is geen internetverbinding", Toast.LENGTH_SHORT).show();
	    			finish();
	        	}
	        	
	        	canBeAnswered=false; //vraag mag niet meer beantwoord worden.
	        	goToResults();
	        }
	    });
	}
	
	@Override
	public void onResume() {
		super.onResume();
		if(!canBeAnswered) {
			this.finish(); // ga ineens terug naar vragenmenu wanneer de vraag reeds beantwoord werd
		}
	}
	
	private void goToResults() {
		Intent i=new Intent(AnswerQuestion.this, QuestionResult.class);
    	i.putExtra("questionId", question.getId());
    	startActivity(i);
	}
}
